<?php

namespace seecms\controller;

use seecms\lib\builder\button\PageButton;
use seecms\lib\builder\column\ImageColumn;
use seecms\lib\builder\control\ImageControl;
use seecms\lib\builder\control\InputControl;
use seecms\lib\builder\control\PasswordControl;
use seecms\lib\builder\modal\Dialog;
use seecms\lib\builder\query\SelectQuery;
use seecms\See;
use seecms\SeeLang;

class UserManager extends Resource
{

    protected $table_name = 'see_user';

    protected $dictionary_name = 'user';

    protected function pageTitle(): string
    {
        return __('user.page_title');
    }

    public function query(): array
    {
        return [
            'username',
            'nickname',
            'phone',
            SelectQuery::init('status')->setOptions(See::provider()->user->statusOptions()),
        ];
    }

    public function columns(): array
    {
        return [
            'id',
            ImageColumn::init('avatar'),
            'username',
            'nickname',
            'phone',
            'email',
            'status',
            'last_login_time',
            'last_login_ip',
            'created_at',
        ];
    }

    public function actions(): array
    {
        return [
            PageButton::init('grant')->setUrl(auth_route('permission/userGrant'))->setDialog(Dialog::init('user_grant')->areaLarge()),
            'read',
            'edit',
            'del'
        ];
    }

    public function readControls(): array
    {
        return [
            'nickname',
            'avatar.false',
            InputControl::init('phone')->setPrefix('layui-icon-cellphone'),
            InputControl::init('email')->setPrefix('layui-icon-email'),
            'created_at',
            'updated_at'
        ];
    }

    public function controls(): array
    {
        return [
            InputControl::init('username')->setDescription(SeeLang::user('username_tip')),
            PasswordControl::init('password')->setDescription(SeeLang::user('password_tip')),
            PasswordControl::init('confirm_password')->setDescription(SeeLang::user('confirm_password_tip')),
            InputControl::init('nickname')->setDescription(SeeLang::user('nickname_tip')),
            ImageControl::init('avatar')->setRequired(),
            InputControl::init('phone')->setRequired()->setPrefix('layui-icon-cellphone'),
            InputControl::init('email')->setRequired()->setPrefix('layui-icon-email'),
        ];
    }

    public function editControls(): array
    {
        return [
            'nickname',
            PasswordControl::init('new_password')->setLabel(SeeLang::user('new_password'))->setDescription(SeeLang::user('new_password_tip'))->setRequired(),
            ImageControl::init('avatar')->setRequired()->setChoose(),
            InputControl::init('phone')->setPrefix('layui-icon-cellphone'),
            InputControl::init('email')->setPrefix('layui-icon-email'),
        ];
    }

    public function delete(): array
    {
        $id = See::request()->param('id');

        if ($id == 1) {
            return $this->fail(SeeLang::user('delete_root'));
        }

        return parent::delete();
    }
}